/*
 * @lc app=leetcode.cn id=56 lang=cpp
 *
 * [56] 合并区间
 */

// @lc code=start
class Solution {
public:
    vector<vector<int>> merge(vector<vector<int>>& intervals) {
        sort(intervals.begin(), intervals.end(), [](vector<int>& a, vector<int>& b) {
            return a[0] == b[0] ? b[1] < a[1] : a[0] < b[0];
        });
        vector<vector<int>> res;
        res.emplace_back(intervals[0]);
        for (int i = 1; i < intervals.size(); ++i) {
            vector<int> cur = intervals[i];
            if (cur[0] <= res.back()[1]) {
                res.back()[1] = max(res.back()[1], cur[1]); 
            } else {
                res.emplace_back(cur);
            }
        }
        return res;
    }
};
// @lc code=end

